package com.zeng.controller;

import com.zeng.service.LogsinfoService;
import com.zeng.service.ManagerinfoService;
import com.zeng.utils.getUserIp;
import com.zeng.entity.Logsinfo;
import com.zeng.entity.Managerinfo;
import com.zeng.entity.Result;
import com.zeng.entity.ResultPage;
import com.zeng.utils.TimeUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * (Managerinfo)表控制层
 *
 * @author zengfanchao
 * @since 2021-03-17 19:17:04
 */
@RestController
@RequestMapping("managerinfo")
public class ManagerinfoController {
    /**
     * 服务对象
     */
    @Resource
    private ManagerinfoService managerinfoService;
    @Resource
    private LogsinfoService logsinfoService;
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Managerinfo selectOne(Integer id) {
        return this.managerinfoService.queryById(id);
    }

    /**
     * 查询全部
     * @return
     */
    @GetMapping("selectAll")
    public List<Managerinfo> selectAll() {
        return this.managerinfoService.selectAll();
    }

    /**
     * 根据页号查询
     * @param offset 查询初始位置
     * @param limit 查询数量
     * @return
     */
    @GetMapping("selectAllByLimit")
    public List<Managerinfo> selectAllByLimit(int offset, int limit) {
        return this.managerinfoService.queryAllByLimit(offset,limit);
    }
    @PostMapping("selectKeyByLimit")
    public ResultPage selectKeyByLimit(@RequestBody HashMap<String, String> map) {
        ResultPage resultPage = new ResultPage();
        if(map.get("key").equals(""))
        {
            int pageTotal = this.managerinfoService.queryCount();
            List<Managerinfo> list = this.managerinfoService.queryAllByLimit(Integer.valueOf(map.get("offset")), Integer.valueOf(map.get("limit")));
            resultPage.setData(list);
            resultPage.setPageTotal(pageTotal);
        }else {
            int pageTotal = this.managerinfoService.queryCountSelect(map.get("key"));
            List<Managerinfo> list = this.managerinfoService.queryKeyByLimit(map.get("key"),Integer.valueOf(map.get("offset")), Integer.valueOf(map.get("limit")));
            resultPage.setData(list);
            resultPage.setPageTotal(pageTotal);
        }
        return resultPage;
    }

    /**
     * 登陆
     * @param map 传输的账号密码
     * @param response
     * @param request
     * @return
     */
    @PostMapping("login")
    public Result login(@RequestBody HashMap<String, String> map, HttpServletResponse response, HttpServletRequest request) {
        Managerinfo managerinfo = managerinfoService.queryByUserName(map.get("username"));
        if (managerinfo == null || !managerinfo.getPwd().equals(map.get("pwd"))) {
            //写日志
            //(String operator, Date createtime, String createip, String details, String note)
            logsinfoService.insert(new Logsinfo(map.get("username"), TimeUtils.NowTimeN(), new getUserIp().getIp(request), "管理员登陆", "登陆失败"));
            return Result.error("用户名或密码错误");
        }
        //写日志
        //(String operator, Date createtime, String createip, String details, String note)
        logsinfoService.insert(new Logsinfo(map.get("username"), TimeUtils.NowTimeN(), new getUserIp().getIp(request), "管理员登陆", "登陆成功"));
        return Result.success("登录成功", managerinfo);
    }
}